package com.aat.dxfy.http;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpReq {

	public static String get(String url) {
		CloseableHttpClient http = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		HttpGet req = new HttpGet(url);
		try {
			response = http.execute(req);
			HttpEntity entity = response.getEntity();
			String res = EntityUtils.toString(entity, "utf-8");
			EntityUtils.consume(entity);
			return res;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (req != null) {
					req.releaseConnection();
				}
				if (response != null) {
					response.close();
				}
				if (http != null) {
					http.close();
				}
			} catch (Exception e) {
			}
		}
		return null;
	}

	public static String post(String url, String json) {
		CloseableHttpClient http = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		HttpPost req = new HttpPost(url);
		try {
			StringEntity entityPost = new StringEntity(json, Charset.forName("utf-8"));
			req.setEntity(entityPost);
			
			response = http.execute(req);
			HttpEntity entity = response.getEntity();
			String res = EntityUtils.toString(entity, "utf-8");
			EntityUtils.consume(entity);
			return res;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (req != null) {
					req.releaseConnection();
				}
				if (response != null) {
					response.close();
				}
				if (http != null) {
					http.close();
				}
			} catch (Exception e) {
			}
		}
		return null;
	}
	
	public static String postForm(String url,Map<String,String> map) {
		CloseableHttpClient http = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		HttpPost req = new HttpPost(url);
		try {
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			for (Entry<String, String> entry : map.entrySet()) {
				nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
			}
			req.setEntity(new UrlEncodedFormEntity(nvps,"UTF-8"));
			
			response = http.execute(req);
			HttpEntity entity = response.getEntity();
			String res = EntityUtils.toString(entity, "utf-8");
			EntityUtils.consume(entity);
			return res;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (req != null) {
					req.releaseConnection();
				}
				if (response != null) {
					response.close();
				}
				if (http != null) {
					http.close();
				}
			} catch (Exception e) {
			}
		}
		return null;
	}

}
